package com.smarttiger.zero.admin.task;

import java.util.Date;

import com.smarttiger.zero.admin.dao.TaskDao;
import com.smarttiger.zero.admin.entity.TaskModel;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;


/**
 * Created by zhouzhengwei on 2021/11/13.
 */
@Configuration
public class QuartzScheduler {

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private TaskDao taskDao;

    /**
     * 开始执行所有任务
     *
     * @throws SchedulerException
     */
    public void startJob(TaskModel task) throws SchedulerException {
        String taskId = task.getId();
        JobDetail jobDetail = JobBuilder.newJob(SchedulerQuartzJob.class)
                .usingJobData("taskId",taskId)
                .usingJobData("taskName",task.getName())
                .withIdentity(task.getCode()).build();
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(task.getCron());
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(task.getCode())
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
        scheduler.start();
        task.setStatus("R");
        taskDao.save(task);
    }

    /**
     * 获取Job信息
     *
     * @param name
     * @return
     * @throws SchedulerException
     */
    public String getJobInfo(String name) throws SchedulerException {
        TriggerKey triggerKey = new TriggerKey(name);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
                scheduler.getTriggerState(triggerKey).name());
    }

    /**
     * 修改某个任务的执行时间
     *
     * @param name
     * @param time
     * @return
     * @throws SchedulerException
     */
    public boolean modifyJob(String name, String time) throws SchedulerException {
        Date date = null;
        TriggerKey triggerKey = new TriggerKey(name);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String oldTime = cronTrigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(time)) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name)
                    .withSchedule(cronScheduleBuilder).build();
            date = scheduler.rescheduleJob(triggerKey, trigger);
        }
        return date != null;
    }

    /**
     * 暂停所有任务
     *
     * @throws SchedulerException
     */
    public void pauseAllJob() throws SchedulerException {
        scheduler.pauseAll();
    }

    /**
     * 暂停某个任务
     *
     * @param name
     * @throws SchedulerException
     */
    public void pauseJob(String name) throws SchedulerException {
        JobKey jobKey = new JobKey(name);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.pauseJob(jobKey);
    }

    /**
     * 恢复所有任务
     *
     * @throws SchedulerException
     */
    public void resumeAllJob() throws SchedulerException {
        scheduler.resumeAll();
    }

    /**
     * 恢复某个任务
     *
     * @param name
     * @throws SchedulerException
     */
    public void resumeJob(String name) throws SchedulerException {
        JobKey jobKey = new JobKey(name);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null)
            return;
        scheduler.resumeJob(jobKey);
    }

    /**
     * 删除某个任务
     *
     * @param name
     * @throws SchedulerException
     */
    public void deleteJob(String name) throws SchedulerException {
        JobKey jobKey = new JobKey(name);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null){
            return;
        }
        scheduler.deleteJob(jobKey);
        TaskModel t = taskDao.findByCode(name);
        t.setStatus("S");
        taskDao.save(t);
    }


}

